package com.dubbo.lian.common.security.redis;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisException;

/** 
 * JedisPool 管理类  
 * 用于单个redis 集群， 每个redis集群由master-salve组成 
 */  
@SuppressWarnings("all")
public class JedisPoolManager {  
  
    private static Log log = LogFactory.getLog(JedisPoolManager.class);  
  
    private JedisPool jedisPool;  

    
	public long rpush(String key, String string) {  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            long ret = jedis.rpush(key, string);  
            jedisPool.returnResource(jedis);  
            return ret;  
        } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
    }  

    public List<String> lrange(String key, long start, long end) {  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            List<String> ret = jedis.lrange(key, start, end);  
            jedisPool.returnResource(jedis);  
            return ret;  
        } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
    }  

    public void hset(String key, String field, String value) {  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            jedis.hset(key, field, value);  
            jedisPool.returnResource(jedis);  
        } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
    }  

    public void set(String key, String value) {  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            jedis.set(key, value);  
            jedisPool.returnResource(jedis);  
        } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
    }  

    public void set(byte[] key, byte[] value) {  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            jedis.set(key, value);  
            jedisPool.returnResource(jedis);  
        } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
    }  

    public String get(String key) {  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            String value = jedis.get(key);  
            jedisPool.returnResource(jedis);  
            return value;  
        } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
    }  

    public byte[] get(byte[] key) {  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            byte[] value = jedis.get(key);  
            jedisPool.returnResource(jedis);  
            return value;  
        } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
  
    }  

    public void hmset(String key, Map<String, String> map) {  
        Jedis jedis = null;  
        try {  
             jedis = jedisPool.getResource();  
            jedis.hmset(key, map);  
            jedisPool.returnResource(jedis);  
        } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
    }  

    public void setex(String key, int seconds, String value) {  
        Jedis jedis = null;  
        try {  
             jedis = jedisPool.getResource();  
            jedis.setex(key, seconds, value);  
            jedisPool.returnResource(jedis);  
         } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
    }  

    public byte[] setex(byte[] key, byte[] value, int seconds) {  
        Jedis jedis = null;  
        try {  
             jedis = jedisPool.getResource();  
            jedis.setex(key, seconds, value);  
            jedisPool.returnResource(jedis);  
            return value;  
        } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
          
  
    }  

    public void expire(String key, int seconds) {  
        Jedis jedis = null;  
        try {  
             jedis = jedisPool.getResource();  
            jedis.expire(key, seconds);  
            jedisPool.returnResource(jedis);  
        } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
             throw new JedisException(e);  
        }  
    }  

    public boolean exists(String key) {  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            boolean bool = jedis.exists(key);  
            jedisPool.returnResource(jedis);  
            return bool;  
        } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
    }  

    public boolean exists(byte[] key) {  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            Set<byte[]> hashSet = jedis.keys(key);  
            jedisPool.returnResource(jedis);  
            if (null != hashSet && hashSet.size() >0 ){  
                return true;  
            }else{  
                return false;  
            }  
  
        } catch (Exception e) {  
             log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
    }  

    public String type(String key) {  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            String type = jedis.type(key);  
            jedisPool.returnResource(jedis);  
            return type;  
        } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
    }  

    public String hget(String key, String field) {  
        Jedis jedis = null;  
        try {  
             jedis = jedisPool.getResource();  
            String value = jedis.hget(key, field);  
            jedisPool.returnResource(jedis);  
            return value;  
        } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
    }  

    public Map<String, String> hgetAll(String key) {  
        Jedis jedis = null;  
        try {  
             jedis = jedisPool.getResource();  
            Map<String, String> map = jedis.hgetAll(key);  
            jedisPool.returnResource(jedis);  
            return map;  
        } catch (Exception e) {  
             log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
  
    }  

    public Set<?> smembers(String key) {  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
             Set<?> set = jedis.smembers(key);  
            jedisPool.returnResource(jedis);  
            return set;  
        } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
             throw new JedisException(e);  
        }  
    }     

     public Set<byte[]> keys(String pattern) {  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            Set<byte[]> keys = jedis.keys(pattern.getBytes());  
            jedisPool.returnResource(jedis);  
            return keys;  
        } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
  
    }  

    public void delSetObj(String key, String field) {  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            jedis.srem(key, field);  
            jedisPool.returnResource(jedis);  
        } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
    }  

    public void del(byte[] key) {  
         Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            jedis.del(key);  
            jedisPool.returnResource(jedis);  
        } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
    }  

    public boolean isNotField(String key, String field) {  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
             boolean bool = jedis.sismember(key, field);  
            jedisPool.returnResource(jedis);  
             return bool;  
        } catch (Exception e) {  
             log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
    }  

    public void append(String key, String value) {  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            jedis.append(key, value);  
            jedisPool.returnResource(jedis);  
        } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
    }  

    public void flushDB() {  
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
            jedis.flushDB();  
            jedisPool.returnResource(jedis);  
        } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
  
     }  

    public Long dbSize() {  
          
        Long dbSize = 0L;  
          
        Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();  
             jedis.dbSize();  
            jedisPool.returnResource(jedis);  
            return dbSize;  
        } catch (Exception e) {  
            log.error(e);  
            if (jedis != null) {  
                jedisPool.returnBrokenResource(jedis);  
            }  
            throw new JedisException(e);  
        }  
  
    }  

    public void destory() {  
        jedisPool.destroy();  
    }  
  
       public JedisPool getJedisPool() {  
        return jedisPool;  
    }  
    public void setJedisPool(JedisPool jedisPool) {  
        this.jedisPool = jedisPool;  
    }  
}  
